<!doctype html>
<html lang="en" data-color-mode="dark">
<head>
<meta charset="utf-8">
<title>npm 备忘清单
 &#x26;  npm cheatsheet &#x26;  Quick Reference</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta description="这个 npm 快速参考备忘单显示了它的常用命令使用清单

常用命令，为开发人员分享快速参考备忘单。">
<meta keywords="npm,reference,Quick,Reference,cheatsheet,cheat,sheet">
<link rel="icon" href="data:image/svg+xml,%3Csvg%20viewBox%3D%220%200%2024%2024%22%20fill%3D%22none%22%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%221em%22%20width%3D%221em%22%3E%20%3Cpath%20d%3D%22m21.66%2010.44-.98%204.18c-.84%203.61-2.5%205.07-5.62%204.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2%201.17-2.42%203.16-3.07%206.5-2.28l1.67.39c4.19.98%205.47%203.05%204.49%207.23Z%22%20fill%3D%22%23c9d1d9%22%2F%3E%20%3Cpath%20d%3D%22M15.06%2019.39c-.62.42-1.4.77-2.35%201.08l-1.58.52c-3.97%201.28-6.06.21-7.35-3.76L2.5%2013.28c-1.28-3.97-.22-6.07%203.75-7.35l1.58-.52c.41-.13.8-.24%201.17-.31-.3.61-.54%201.35-.74%202.2l-.98%204.19c-.98%204.18.31%206.24%204.48%207.23l1.68.4c.58.14%201.12.23%201.62.27Zm2.43-8.88c-.06%200-.12-.01-.19-.02l-4.85-1.23a.75.75%200%200%201%20.37-1.45l4.85%201.23a.748.748%200%200%201-.18%201.47Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3Cpath%20d%3D%22M14.56%2013.89c-.06%200-.12-.01-.19-.02l-2.91-.74a.75.75%200%200%201%20.37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z%22%20fill%3D%22%23228e6c%22%20%2F%3E%20%3C%2Fsvg%3E" type="image/svg+xml">
<link rel="stylesheet" href="../style/style.css">
<link rel="stylesheet" href="../style/katex.css">
</head>
<body><nav class="header-nav"><div class="max-container"><a href="../index.html" class="logo"><svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg" height="1em" width="1em">
  <path d="m21.66 10.44-.98 4.18c-.84 3.61-2.5 5.07-5.62 4.77-.5-.04-1.04-.13-1.62-.27l-1.68-.4c-4.17-.99-5.46-3.05-4.48-7.23l.98-4.19c.2-.85.44-1.59.74-2.2 1.17-2.42 3.16-3.07 6.5-2.28l1.67.39c4.19.98 5.47 3.05 4.49 7.23Z" fill="#c9d1d9"></path>
  <path d="M15.06 19.39c-.62.42-1.4.77-2.35 1.08l-1.58.52c-3.97 1.28-6.06.21-7.35-3.76L2.5 13.28c-1.28-3.97-.22-6.07 3.75-7.35l1.58-.52c.41-.13.8-.24 1.17-.31-.3.61-.54 1.35-.74 2.2l-.98 4.19c-.98 4.18.31 6.24 4.48 7.23l1.68.4c.58.14 1.12.23 1.62.27Zm2.43-8.88c-.06 0-.12-.01-.19-.02l-4.85-1.23a.75.75 0 0 1 .37-1.45l4.85 1.23a.748.748 0 0 1-.18 1.47Z" fill="#228e6c"></path>
  <path d="M14.56 13.89c-.06 0-.12-.01-.19-.02l-2.91-.74a.75.75 0 0 1 .37-1.45l2.91.74c.4.1.64.51.54.91-.08.34-.38.56-.72.56Z" fill="#228e6c"></path>
</svg>
<span class="title">Quick Reference</span></a><div class="menu"><a href="javascript:void(0);" class="searchbtn" id="searchbtn"><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><span>搜索</span><span>⌘K</span></a><a href="https://github.com/jaywcjlove/reference/blob/main/docs/npm.md" class="" target="__blank"><svg viewBox="0 0 36 36" fill="currentColor" height="1em" width="1em"><path d="m33 6.4-3.7-3.7a1.71 1.71 0 0 0-2.36 0L23.65 6H6a2 2 0 0 0-2 2v22a2 2 0 0 0 2 2h22a2 2 0 0 0 2-2V11.76l3-3a1.67 1.67 0 0 0 0-2.36ZM18.83 20.13l-4.19.93 1-4.15 9.55-9.57 3.23 3.23ZM29.5 9.43 26.27 6.2l1.85-1.85 3.23 3.23Z"></path><path fill="none" d="M0 0h36v36H0z"></path></svg><span>编辑</span></a><button id="darkMode" type="button"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="currentColor" class="light" height="1em" width="1em">
  <path d="M6.995 12c0 2.761 2.246 5.007 5.007 5.007s5.007-2.246 5.007-5.007-2.246-5.007-5.007-5.007S6.995 9.239 6.995 12zM11 19h2v3h-2zm0-17h2v3h-2zm-9 9h3v2H2zm17 0h3v2h-3zM5.637 19.778l-1.414-1.414 2.121-2.121 1.414 1.414zM16.242 6.344l2.122-2.122 1.414 1.414-2.122 2.122zM6.344 7.759 4.223 5.637l1.415-1.414 2.12 2.122zm13.434 10.605-1.414 1.414-2.122-2.122 1.414-1.414z"></path>
</svg>
<svg xmlns="http://www.w3.org/2000/svg" fill="currentColor" viewBox="0 0 24 24" class="dark" height="1em" width="1em">
  <path d="M12 11.807A9.002 9.002 0 0 1 10.049 2a9.942 9.942 0 0 0-5.12 2.735c-3.905 3.905-3.905 10.237 0 14.142 3.906 3.906 10.237 3.905 14.143 0a9.946 9.946 0 0 0 2.735-5.119A9.003 9.003 0 0 1 12 11.807z"></path>
</svg>
</button><script src="../js/dark.js?v=1.5.2"></script><a href="https://github.com/jaywcjlove/reference" class="" target="__blank"><svg viewBox="0 0 16 16" fill="currentColor" height="1em" width="1em"><path d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.012 8.012 0 0 0 16 8c0-4.42-3.58-8-8-8z"></path></svg></a></div></div></nav><div class="wrap h1body-exist max-container"><header class="wrap-header h1wrap"><h1 id="npm-备忘清单"><svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" fill="currentColor" height="1em" width="1em">
  <path d="M1.763 0C.786 0 0 .786 0 1.763v20.474C0 23.214.786 24 1.763 24h20.474c.977 0 1.763-.786 1.763-1.763V1.763C24 .786 23.214 0 22.237 0zM5.13 5.323l13.837.019-.009 13.836h-3.464l.01-10.382h-3.456L12.04 19.17H5.113z"></path>
</svg>
<a aria-hidden="true" tabindex="-1" href="#npm-备忘清单"><span class="icon icon-link"></span></a>npm 备忘清单</h1><div class="wrap-body">
<p>这个 <a href="https://www.npmjs.com/">npm</a> 快速参考备忘单显示了它的常用命令使用清单</p>
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
  <path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#常用命令">常用命令</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#包管理">包管理</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#安装">安装</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#清单">清单</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#缓存-cache">缓存 cache</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#更新">更新</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#杂项功能">杂项功能</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#取消发布包">取消发布包</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#更改包裹可见性">更改包裹可见性</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#要将包转移到-npm-用户帐户">要将包转移到 npm 用户帐户</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#发布包-npmjsorg">发布包 npmjs.org</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#使用-nrm-切换-registry">使用 nrm 切换 registry</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#init">init</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#exec">exec</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#npx">npx</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#介绍">介绍</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#npx-vs-npm-exec">npx VS npm exec</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#npx-vs-npm-exec-示例">npx VS npm exec 示例</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#创建一个-react-naive-项目">创建一个 React Naive 项目</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#创建一个-react-应用">创建一个 React 应用</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#配置">配置</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#npmrc">.npmrc</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#配置内容">配置内容</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#registry">registry</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#npmignore">.npmignore</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#中国镜像站安装">中国镜像站安装</a><a aria-hidden="true" class="leve4 tocs-link" data-num="4" href="#electronjs-镜像和缓存">electronjs 镜像和缓存</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#身份验证相关配置">身份验证相关配置</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#纯-esm-包">纯 ESM 包</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#commonjs-项目移动到-esm">CommonJS 项目移动到 ESM</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#typescript-项目输出-esm">TypeScript 项目输出 ESM</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="常用命令"><a aria-hidden="true" tabindex="-1" href="#常用命令"><span class="icon icon-link"></span></a>常用命令</h2><div class="wrap-body">
<!--rehype:body-class=cols-2-->
</div></div><div class="h2wrap-body cols-2"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="包管理"><a aria-hidden="true" tabindex="-1" href="#包管理"><span class="icon icon-link"></span></a>包管理</h3><div class="wrap-body">





















































<table class="auto-wrap"><thead><tr><th align="left">命令</th><th align="left">描述</th></tr></thead><tbody><tr><td align="left"><code>npm init -y</code></td><td align="left">创建 <code>package.json</code> 文件</td></tr><tr><td align="left"><code>npm install</code> 或 <code>npm i</code></td><td align="left">安装 <code>package.json</code> 中的所有内容</td></tr><tr><td align="left"><code>npm install --production</code></td><td align="left">安装 <code>package.json</code> 中的所有内容 <br> <em>(除了 <code>devDependecies</code>)</em></td></tr><tr><td align="left"><code>npm install lodash</code></td><td align="left">安装一个包</td></tr><tr><td align="left"><code>npm install --save-dev lodash</code></td><td align="left">安装为 <code>devDependency</code></td></tr><tr><td align="left"><code>npm install --save-exact lodash</code></td><td align="left">准确安装</td></tr><tr><td align="left"><code>npm install @scope/package-name</code></td><td align="left">安装一个作用域的公共包</td></tr><tr><td align="left"><code>npm install &#x3C;package_name>@&#x3C;tag></code></td><td align="left">使用 <code>dist-tags</code> 安装包</td></tr><tr><td align="left"><code>npm install -g &#x3C;package_name></code></td><td align="left">全局安装包</td></tr><tr><td align="left"><code>npm uninstall &#x3C;package_name></code></td><td align="left">卸载包</td></tr><tr><td align="left"><code>npm uninstall -g &#x3C;package_name></code></td><td align="left">全局卸载包</td></tr></tbody></table>
<!--rehype:class=auto-wrap-->
</div></div></div><div class="wrap h3body-not-exist row-span-3"><div class="wrap-header h3wrap"><h3 id="安装"><a aria-hidden="true" tabindex="-1" href="#安装"><span class="icon icon-link"></span></a>安装</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-3-->

























































<table class="auto-wrap"><thead><tr><th align="left">命令</th><th align="left">描述</th></tr></thead><tbody><tr><td align="left"><code>npm i sax</code></td><td align="left"><code>NPM</code> 包</td></tr><tr><td align="left"><code>npm i sax@latest</code></td><td align="left">指定标签 <code>最新</code></td></tr><tr><td align="left"><code>npm i sax@3.0.0</code></td><td align="left">指定版本 <code>3.0.0</code></td></tr><tr><td align="left"><code>npm i sax@">=1 &#x3C;2.0"</code></td><td align="left">指定版本范围</td></tr><tr><td align="left"><code>npm i @org/sax</code></td><td align="left">范围内的 <code>NPM</code> 包</td></tr><tr><td align="left"><code>npm i user/repo</code></td><td align="left">GitHub</td></tr><tr><td align="left"><code>npm i user/repo#master</code></td><td align="left">GitHub</td></tr><tr><td align="left"><code>npm i github:user/repo</code></td><td align="left">GitHub</td></tr><tr><td align="left"><code>npm i gitlab:user/repo</code></td><td align="left">GitLab</td></tr><tr><td align="left"><code>npm i /path/to/repo</code></td><td align="left">绝对路径</td></tr><tr><td align="left"><code>npm i ./archive.tgz</code></td><td align="left">压缩包</td></tr><tr><td align="left"><code>npm i https://site.com/archive.tgz</code></td><td align="left">通过 <code>HTTP</code> 压缩包</td></tr></tbody></table>
<!--rehype:class=auto-wrap-->
<p>安装依赖的可用参数</p>
<ul class="style-arrow">
<li><code>-P</code>, <code>--save-prod</code> 包将出现在您的依赖项中，这是默认值(npm v8)，除非存在 <code>-D</code> 或 <code>-O</code></li>
<li><code>-D</code>, <code>--save-dev</code> 包会出现在你的 <code>devDependencies</code> 中</li>
<li><code>-O</code>, <code>--save-optional</code> 包将出现在您的 <code>optionalDependencies</code> 中</li>
<li><code>--no-save</code> 防止保存到依赖项</li>
<li><code>-E</code>, <code>--save-exact</code> 依赖项将使用精确的版本进行配置，而不是使用 <code>npm</code> 的默认 <a href="./semver.html"><code>semver</code></a> 范围运算符</li>
<li><code>-B</code>, <code>--save-bundle</code> 依赖项也将添加到您的 <a href="./package.json.html#bundleddependencies"><code>bundleDependencies</code></a> 列表中</li>
</ul>
<!--rehype:className=style-arrow-->
<p>命令 <code>npm i</code> 是 <code>npm install</code> 的别名</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="清单"><a aria-hidden="true" tabindex="-1" href="#清单"><span class="icon icon-link"></span></a>清单</h3><div class="wrap-body">

























<table class="auto-wrap"><thead><tr><th align="left">命令</th><th align="left">描述</th></tr></thead><tbody><tr><td align="left"><code>npm list</code></td><td align="left">列出此软件中所有依赖项的已安装版本</td></tr><tr><td align="left"><code>npm list -g --depth 0</code></td><td align="left">列出所有全局安装包的安装版本</td></tr><tr><td align="left"><code>npm view</code></td><td align="left">列出此软件中所有依赖项的最新版本</td></tr><tr><td align="left"><code>npm outdated</code></td><td align="left">仅列出此软件中已过时的依赖项</td></tr></tbody></table>
<!--rehype:class=auto-wrap-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="缓存-cache"><a aria-hidden="true" tabindex="-1" href="#缓存-cache"><span class="icon icon-link"></span></a>缓存 cache</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> cache <span class="token function">add</span> <span class="token operator">&#x3C;</span>package-spec<span class="token operator">></span>    <span class="token comment"># 将指定的包添加到本地缓存</span>
</span><span class="code-line">$ <span class="token function">npm</span> cache clean <span class="token punctuation">[</span><span class="token operator">&#x3C;</span>key<span class="token operator">></span><span class="token punctuation">]</span>         <span class="token comment"># 删除缓存文件夹中的所有数据</span>
</span><span class="code-line">$ <span class="token function">npm</span> cache <span class="token function">ls</span> <span class="token punctuation">[</span><span class="token operator">&#x3C;</span>name<span class="token operator">></span>@<span class="token operator">&#x3C;</span>version<span class="token operator">></span><span class="token punctuation">]</span>
</span><span class="code-line">$ <span class="token function">npm</span> cache verify <span class="token comment"># 验证缓存文件夹的内容，垃圾收集任何不需要的数据，</span>
</span><span class="code-line">                 <span class="token comment"># 并验证缓存索引和所有缓存数据的完整性</span>
</span></code></pre>
<p>用于添加、列出或清理 <a href="https://www.npmjs.com/">npm</a> 缓存文件夹</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="更新"><a aria-hidden="true" tabindex="-1" href="#更新"><span class="icon icon-link"></span></a>更新</h3><div class="wrap-body">





























<table><thead><tr><th align="left">命令</th><th>描述</th></tr></thead><tbody><tr><td align="left"><code>npm version &#x3C;version></code></td><td>要更改 <code>package.json</code> 中的版本号</td></tr><tr><td align="left"><code>npm update</code></td><td>更新生产包</td></tr><tr><td align="left"><code>npm update --dev</code></td><td>更新开发包</td></tr><tr><td align="left"><code>npm update -g</code></td><td>更新全局包</td></tr><tr><td align="left"><code>npm update lodash</code></td><td>更新 <code>lodash</code> 包</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist row-span-2"><div class="wrap-header h3wrap"><h3 id="杂项功能"><a aria-hidden="true" tabindex="-1" href="#杂项功能"><span class="icon icon-link"></span></a>杂项功能</h3><div class="wrap-body">
<!--rehype:wrap-class=row-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 将某人添加为所有者</span>
</span><span class="code-line">$ <span class="token function">npm</span> owner <span class="token function">add</span> USERNAME PACKAGENAME
</span><span class="code-line"><span class="token comment"># 列出包</span>
</span><span class="code-line">$ <span class="token function">npm</span> <span class="token function">ls</span>
</span><span class="code-line"><span class="token comment"># 向安装旧版本软件包的用户添加警告(弃用)</span>
</span><span class="code-line">$ <span class="token function">npm</span> deprecate PACKAGE@<span class="token string">"&#x3C; 0.2.0"</span> <span class="token string">"critical bug fixed in v0.2.0"</span>
</span><span class="code-line"><span class="token comment"># 更新所有包或选定的包</span>
</span><span class="code-line">$ <span class="token function">npm</span> update <span class="token punctuation">[</span>-g<span class="token punctuation">]</span> PACKAGE
</span><span class="code-line"><span class="token comment"># 检查过时的包</span>
</span><span class="code-line">$ <span class="token function">npm</span> outdated <span class="token punctuation">[</span>PACKAGE<span class="token punctuation">]</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="取消发布包"><a aria-hidden="true" tabindex="-1" href="#取消发布包"><span class="icon icon-link"></span></a>取消发布包</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> unpublish <span class="token operator">&#x3C;</span>package-name<span class="token operator">></span> <span class="token parameter variable">-f</span>
</span><span class="code-line"><span class="token comment"># 取消指定版本</span>
</span><span class="code-line">$ <span class="token function">npm</span> unpublish <span class="token operator">&#x3C;</span>package-name<span class="token operator">></span>@<span class="token operator">&#x3C;</span>version<span class="token operator">></span>
</span></code></pre>
<p>注意：如果您取消发布整个包，则必须在 24 小时后才能发布该包的任何新版本</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="更改包裹可见性"><a aria-hidden="true" tabindex="-1" href="#更改包裹可见性"><span class="icon icon-link"></span></a>更改包裹可见性</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 将公共包设为私有</span>
</span><span class="code-line">$ <span class="token function">npm</span> access restricted <span class="token operator">&#x3C;</span>package-name<span class="token operator">></span>
</span><span class="code-line"><span class="token comment"># 公开私有包</span>
</span><span class="code-line">$ <span class="token function">npm</span> access public <span class="token operator">&#x3C;</span>package-name<span class="token operator">></span>
</span><span class="code-line"><span class="token comment"># 授予私有包访问权限</span>
</span><span class="code-line">$ <span class="token function">npm</span> owner <span class="token function">add</span> <span class="token operator">&#x3C;</span>user<span class="token operator">></span> <span class="token operator">&#x3C;</span>your-package-name<span class="token operator">></span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="要将包转移到-npm-用户帐户"><a aria-hidden="true" tabindex="-1" href="#要将包转移到-npm-用户帐户"><span class="icon icon-link"></span></a>要将包转移到 npm 用户帐户</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 新维护者接受邀请</span>
</span><span class="code-line">$ <span class="token function">npm</span> owner <span class="token function">add</span> <span class="token operator">&#x3C;</span>their-username<span class="token operator">></span> <span class="token operator">&#x3C;</span>package-name<span class="token operator">></span>
</span><span class="code-line"><span class="token comment"># 删除维护者</span>
</span><span class="code-line">$ <span class="token function">npm</span> owner <span class="token function">rm</span> <span class="token operator">&#x3C;</span>your-username<span class="token operator">></span> <span class="token operator">&#x3C;</span>package-name<span class="token operator">></span>
</span><span class="code-line"><span class="token comment"># 写入启用了双因素身份验证</span>
</span><span class="code-line">$ <span class="token function">npm</span> owner <span class="token function">add</span> <span class="token operator">&#x3C;</span>their-username<span class="token operator">></span> <span class="token operator">&#x3C;</span>package-name<span class="token operator">></span> <span class="token parameter variable">--otp</span><span class="token operator">=</span><span class="token number">123456</span>
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="发布包-npmjsorg"><a aria-hidden="true" tabindex="-1" href="#发布包-npmjsorg"><span class="icon icon-link"></span></a>发布包 npmjs.org</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> publish
</span><span class="code-line"><span class="token comment"># 第一次需要指定公开参数</span>
</span><span class="code-line">$ <span class="token function">npm</span> publish <span class="token parameter variable">--access</span> public
</span><span class="code-line">$ <span class="token function">npm</span> publish <span class="token parameter variable">--access</span> public <span class="token parameter variable">--tag</span> previous
</span></code></pre>
<p>发布公开包，到 <a href="https://docs.npmjs.com">npmjs.org</a></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="使用-nrm-切换-registry"><a aria-hidden="true" tabindex="-1" href="#使用-nrm-切换-registry"><span class="icon icon-link"></span></a>使用 nrm 切换 registry</h3><div class="wrap-body">
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> <span class="token function">install</span> <span class="token parameter variable">-g</span> nrm <span class="token comment"># 安装 nrm 包</span>
</span><span class="code-line"><span class="token comment"># 查看 registry 列表</span>
</span><span class="code-line">$ nrm <span class="token function">ls</span>             
</span><span class="code-line"><span class="token comment"># 将注册表切换到 cnpm</span>
</span><span class="code-line">$ nrm use cnpm       
</span></code></pre>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="init"><a aria-hidden="true" tabindex="-1" href="#init"><span class="icon icon-link"></span></a>init</h3><div class="wrap-body">
<p>用于设置新的或现有的 <code>npm</code> 包</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> init <span class="token operator">&#x3C;</span>package-spec<span class="token operator">></span> <span class="token comment"># (如同 `npx &#x3C;package-spec>)</span>
</span><span class="code-line">$ <span class="token function">npm</span> init <span class="token operator">&#x3C;</span>@scope<span class="token operator">></span>       <span class="token comment"># (如同 `npx &#x3C;@scope>/create`)</span>
</span></code></pre>
<p>别名: <code>create</code>, <code>innit</code></p>





























<table class="left-align"><thead><tr><th align="left">:-</th><th>--</th></tr></thead><tbody><tr><td align="left"><code>npm init foo</code></td><td><code>npm exec create-foo</code></td></tr><tr><td align="left"><code>npm init @usr/foo</code></td><td><code>npm exec @usr/create-foo</code></td></tr><tr><td align="left"><code>npm init @usr</code></td><td><code>npm exec @usr/create</code></td></tr><tr><td align="left"><code>npm init @usr@2.0.0</code></td><td><code>npm exec @usr/create@2.0.0</code></td></tr><tr><td align="left"><code>npm init @usr/foo@2.0.0</code></td><td><code>npm exec @usr/create-foo@2.0.0</code></td></tr></tbody></table>
<!--rehype:className=left-align-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="exec"><a aria-hidden="true" tabindex="-1" href="#exec"><span class="icon icon-link"></span></a>exec</h3><div class="wrap-body">
<p>命令允许您在与通过 <code>npm run</code> 运行它类似的上下文中从 npm 包</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> <span class="token builtin class-name">exec</span> -- <span class="token operator">&#x3C;</span>pkg<span class="token operator">></span><span class="token punctuation">[</span>@<span class="token operator">&#x3C;</span>version<span class="token operator">></span><span class="token punctuation">]</span> <span class="token punctuation">[</span>args<span class="token punctuation">..</span>.<span class="token punctuation">]</span>
</span><span class="code-line">$ <span class="token function">npm</span> <span class="token builtin class-name">exec</span> <span class="token parameter variable">--package</span><span class="token operator">=</span><span class="token operator">&#x3C;</span>pkg<span class="token operator">></span><span class="token punctuation">[</span>@<span class="token operator">&#x3C;</span>version<span class="token operator">></span><span class="token punctuation">]</span> -- <span class="token operator">&#x3C;</span>cmd<span class="token operator">></span> <span class="token punctuation">[</span>args<span class="token punctuation">..</span>.<span class="token punctuation">]</span>
</span><span class="code-line">$ <span class="token function">npm</span> <span class="token builtin class-name">exec</span> <span class="token parameter variable">-c</span> <span class="token string">'&#x3C;cmd> [args...]'</span>
</span><span class="code-line">$ <span class="token function">npm</span> <span class="token builtin class-name">exec</span> <span class="token parameter variable">--package</span><span class="token operator">=</span>foo <span class="token parameter variable">-c</span> <span class="token string">'&#x3C;cmd> [args...]'</span>
</span></code></pre>
<p>别名: x</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> <span class="token builtin class-name">exec</span> <span class="token parameter variable">--package</span> yo <span class="token parameter variable">--package</span> generator-node <span class="token parameter variable">--call</span> <span class="token string">"yo node"</span>
</span><span class="code-line">
</span><span class="code-line">$ <span class="token function">npm</span> <span class="token builtin class-name">exec</span> <span class="token parameter variable">--package</span><span class="token operator">=</span>foo -- bar --bar-argument
</span><span class="code-line"><span class="token comment"># ~ or ~</span>
</span><span class="code-line">$ npx <span class="token parameter variable">--package</span><span class="token operator">=</span>foo bar --bar-argument
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="npx"><a aria-hidden="true" tabindex="-1" href="#npx"><span class="icon icon-link"></span></a>npx</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="介绍"><a aria-hidden="true" tabindex="-1" href="#介绍"><span class="icon icon-link"></span></a>介绍</h3><div class="wrap-body">
<p>从本地或远程 npm 包运行命令</p>
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">npx -- <span class="token operator">&#x3C;</span>pkg<span class="token operator">></span><span class="token punctuation">[</span>@<span class="token operator">&#x3C;</span>version<span class="token operator">></span><span class="token punctuation">]</span> <span class="token punctuation">[</span>args<span class="token punctuation">..</span>.<span class="token punctuation">]</span>
</span><span class="code-line">npx <span class="token parameter variable">--package</span><span class="token operator">=</span><span class="token operator">&#x3C;</span>pkg<span class="token operator">></span><span class="token punctuation">[</span>@<span class="token operator">&#x3C;</span>version<span class="token operator">></span><span class="token punctuation">]</span> -- <span class="token operator">&#x3C;</span>cmd<span class="token operator">></span> <span class="token punctuation">[</span>args<span class="token punctuation">..</span>.<span class="token punctuation">]</span>
</span><span class="code-line">npx <span class="token parameter variable">-c</span> <span class="token string">'&#x3C;cmd> [args...]'</span>
</span><span class="code-line">npx <span class="token parameter variable">--package</span><span class="token operator">=</span>foo <span class="token parameter variable">-c</span> <span class="token string">'&#x3C;cmd> [args...]'</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
<p><code>npx</code> 二进制文件在 <code>npm v7.0.0</code> 中被重写，并且当时不推荐使用独立的 <code>npx</code> 包</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> <span class="token function">install</span> eslint
</span><span class="code-line"><span class="token comment"># 运行：</span>
</span><span class="code-line">$ ./node_modules/.bin/eslint
</span></code></pre>
<p>上面命令简化，直接运行下面👇命令</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ npx eslint
</span></code></pre>
<p>命令 <code>npx</code> 将自动安装并运行 <code>eslint</code></p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="npx-vs-npm-exec"><a aria-hidden="true" tabindex="-1" href="#npx-vs-npm-exec"><span class="icon icon-link"></span></a>npx VS npm exec</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ npx foo@latest bar <span class="token parameter variable">--package</span><span class="token operator">=</span>@npmcli/foo
</span><span class="code-line"><span class="token comment"># npm 将解析 foo 包名，并运行以下命令：</span>
</span><span class="code-line">$ foo bar <span class="token parameter variable">--package</span><span class="token operator">=</span>@npmcli/foo
</span></code></pre>
<!--rehype:className=wrap-text-->
<p>由于 npm 的参数解析逻辑，运行这个命令是不同的:</p>
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> <span class="token builtin class-name">exec</span> foo@latest bar <span class="token parameter variable">--package</span><span class="token operator">=</span>@npmcli/foo
</span><span class="code-line"><span class="token comment"># npm 将首先解析 --package 选项</span>
</span><span class="code-line"><span class="token comment"># 解析 @npmcli/foo 包</span>
</span><span class="code-line"><span class="token comment"># 然后，它将在该上下文中执行以下命令：</span>
</span><span class="code-line">$ foo@latest bar
</span></code></pre>
<!--rehype:className=wrap-text-->
<p>下面命令是与 <code>npx</code> 等效的</p>
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> <span class="token builtin class-name">exec</span> -- foo@latest bar <span class="token parameter variable">--package</span><span class="token operator">=</span>@npmcli/foo
</span><span class="code-line"><span class="token comment"># 等效的</span>
</span><span class="code-line">$ npx foo@latest bar <span class="token parameter variable">--package</span><span class="token operator">=</span>@npmcli/foo
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="npx-vs-npm-exec-示例"><a aria-hidden="true" tabindex="-1" href="#npx-vs-npm-exec-示例"><span class="icon icon-link"></span></a>npx VS npm exec 示例</h3><div class="wrap-body">
<p>使用提供的参数在本地依赖项中运行 <code>tap</code> 版本：</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> <span class="token builtin class-name">exec</span> -- tap <span class="token parameter variable">--bail</span> test/foo.js
</span><span class="code-line">$ npx tap <span class="token parameter variable">--bail</span> test/foo.js
</span></code></pre>
<p>通过指定 <code>--package</code> 选项运行名称与包名称匹配的命令以外的命令：</p>
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> <span class="token builtin class-name">exec</span> <span class="token parameter variable">--package</span><span class="token operator">=</span>foo -- bar --bar-argument
</span><span class="code-line"><span class="token comment"># ~ or ~</span>
</span><span class="code-line">$ npx <span class="token parameter variable">--package</span><span class="token operator">=</span>foo bar --bar-argument
</span></code></pre>
<!--rehype:className=wrap-text-->
<p>在当前项目的上下文中运行任意 <code>shell</code> 脚本：</p>
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> x <span class="token parameter variable">-c</span> <span class="token string">'eslint &#x26;&#x26; say "hooray, lint passed"'</span>
</span><span class="code-line">$ npx <span class="token parameter variable">-c</span> <span class="token string">'eslint &#x26;&#x26; say "hooray, lint passed"'</span>
</span></code></pre>
<!--rehype:className=wrap-text-->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="创建一个-react-naive-项目"><a aria-hidden="true" tabindex="-1" href="#创建一个-react-naive-项目"><span class="icon icon-link"></span></a>创建一个 React Naive 项目</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ npx react-native init AwesomeProject
</span><span class="code-line">$ npx react-native init AwesomeTSProject <span class="token parameter variable">--template</span> react-native-template-typescript
</span></code></pre>
<!--rehype:className=wrap-text-->
<p>使用 <code>npx</code> 直接创建一个  <a href="https://reactnative.dev/docs/environment-setup#creating-a-new-application">React Native</a> 应用</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="创建一个-react-应用"><a aria-hidden="true" tabindex="-1" href="#创建一个-react-应用"><span class="icon icon-link"></span></a>创建一个 React 应用</h3><div class="wrap-body">
<pre class="wrap-text"><code class="language-bash code-highlight"><span class="code-line">$ npx create-react-app my-app
</span><span class="code-line">$ npx create-react-app my-app <span class="token parameter variable">--template</span> typescript
</span></code></pre>
<!--rehype:className=wrap-text-->
<p>使用 <code>npx</code> 跳过安装 <a href="https://reactnative.dev/docs/environment-setup#creating-a-new-application">CRA</a>，直接创建一个 <a href="./react.html">React</a> 应用</p>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="配置"><a aria-hidden="true" tabindex="-1" href="#配置"><span class="icon icon-link"></span></a>配置</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="npmrc"><a aria-hidden="true" tabindex="-1" href="#npmrc"><span class="icon icon-link"></span></a>.npmrc</h3><div class="wrap-body">

























<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>/path/to/project/.npmrc</code></td><td align="left">每个项目的配置文件</td></tr><tr><td align="left"><code>~/.npmrc</code></td><td align="left">每个用户的配置文件</td></tr><tr><td align="left"><code>$PREFIX/etc/npmrc</code></td><td align="left">全局配置文件</td></tr><tr><td align="left"><code>/path/to/npm/npmrc</code></td><td align="left">npm 内置配置文件</td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="配置内容"><a aria-hidden="true" tabindex="-1" href="#配置内容"><span class="icon icon-link"></span></a>配置内容</h3><div class="wrap-body">
<pre class="wrap-text "><code class="language-ini code-highlight"><span class="code-line"><span class="token comment"># last modified: 01 Jan 2016</span>
</span><span class="code-line"><span class="token comment">; Set a new registry for a scoped package</span>
</span><span class="code-line"><span class="token key attr-name">@myscope:registry</span><span class="token punctuation">=</span><span class="token value attr-value">https://registry.npmmirror.com</span>
</span></code></pre>
<!--rehype:className=wrap-text -->
<p>注释使用 <code>#</code>, <code>;</code> 放置到一行的开头， <a href="https://docs.npmjs.com/cli/v8/configuring-npm/npmrc"><code>.npmrc</code></a> 文件由指定此注释语法的 <a href="https://github.com/npm/ini"><code>npm/ini</code></a> 解析</p>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="registry"><a aria-hidden="true" tabindex="-1" href="#registry"><span class="icon icon-link"></span></a>registry</h3><div class="wrap-body">

































<table><thead><tr><th align="left">:-</th><th align="left">:-</th></tr></thead><tbody><tr><td align="left"><code>npm</code></td><td align="left"><a href="https://registry.npmjs.org/">https://registry.npmjs.org/</a></td></tr><tr><td align="left"><code>yarn</code></td><td align="left"><a href="https://registry.yarnpkg.com/">https://registry.yarnpkg.com/</a></td></tr><tr><td align="left"><code>tencent</code></td><td align="left"><a href="https://mirrors.cloud.tencent.com/npm/">https://mirrors.cloud.tencent.com/npm/</a></td></tr><tr><td align="left"><code>cnpm</code></td><td align="left"><a href="https://r.cnpmjs.org/">https://r.cnpmjs.org/</a></td></tr><tr><td align="left"><code>taobao</code></td><td align="left"><a href="https://registry.npmmirror.com/">https://registry.npmmirror.com/</a></td></tr><tr><td align="left"><code>npmMirror</code></td><td align="left"><a href="https://skimdb.npmjs.com/registry/">https://skimdb.npmjs.com/registry/</a></td></tr></tbody></table>
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="npmignore"><a aria-hidden="true" tabindex="-1" href="#npmignore"><span class="icon icon-link"></span></a><code>.npmignore</code></h3><div class="wrap-body">
<p>将下面内容存放到 <code>.npmignore</code> 文件中，放置在项目的根目录中。</p>
<pre class="language-gitignore"><code class="language-gitignore code-highlight"><span class="code-line"><span class="token entry string">.git</span>
</span><span class="code-line"><span class="token entry string">.svn</span>
</span><span class="code-line"><span class="token comment"># 忽略 .swp 后缀的文件</span>
</span><span class="code-line"><span class="token entry string">.<span class="token operator">*</span>.swp</span>
</span><span class="code-line"><span class="token entry string"><span class="token punctuation">/</span>logs<span class="token punctuation">/</span><span class="token operator">*</span></span>
</span><span class="code-line">
</span><span class="code-line"><span class="token comment"># “！” 意思是不要忽视</span>
</span><span class="code-line"><span class="token entry string"><span class="token operator">!</span>logs<span class="token punctuation">/</span>.gitkeep</span>
</span></code></pre>
<p><code>.npmignore</code> 文件就像 <a href="./git.html#%E5%BF%BD%E7%95%A5%E6%96%87%E4%BB%B6"><code>.gitignore</code></a> 一样工作。它不能覆盖 <code>package.json#files</code> 字段。</p>
</div></div></div><div class="wrap h3body-exist col-span-2 row-span-2"><div class="wrap-header h3wrap"><h3 id="中国镜像站安装"><a aria-hidden="true" tabindex="-1" href="#中国镜像站安装"><span class="icon icon-link"></span></a>中国镜像站安装</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-2 row-span-2-->
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line"><span class="token comment"># 临时使用</span>
</span><span class="code-line">$ <span class="token function">npm</span> <span class="token function">install</span> <span class="token parameter variable">-g</span> <span class="token operator">&#x3C;</span>package-name<span class="token operator">></span> <span class="token parameter variable">--registry</span><span class="token operator">=</span>https://registry.npmmirror.com
</span></code></pre>
<p>将配置放置在 <a href="https://docs.npmjs.com/cli/v8/configuring-npm/npmrc"><code>.npmrc</code></a> 全局配置文件中，或者在项目的根目录中。</p>
<pre class="language-ini"><code class="language-ini code-highlight"><span class="code-line"><span class="token comment">; registry=https://registry.npmjs.org/</span>
</span><span class="code-line"><span class="token key attr-name">registry</span><span class="token punctuation">=</span><span class="token value attr-value">https://registry.npmmirror.com</span>
</span></code></pre>
<p>或者配置到 <a href="./package.json.html#publishconfig"><code>package.json#publishConfig</code></a> 字段上</p>
<pre class="language-json"><code class="language-json code-highlight"><span class="code-line"><span class="token property">"publishConfig"</span><span class="token operator">:</span><span class="token punctuation">{</span>
</span><span class="code-line">  <span class="token property">"registry"</span><span class="token operator">:</span> <span class="token string">"https://registry.npmmirror.com"</span>
</span><span class="code-line"><span class="token punctuation">}</span>
</span></code></pre>
<p>替换 npm 仓库地址为 npmmirror(淘宝) 镜像地址</p>
<pre class="language-bash"><code class="language-bash code-highlight"><span class="code-line">$ <span class="token function">npm</span> config <span class="token builtin class-name">set</span> registry https://registry.npmmirror.com
</span></code></pre>
<p>请参阅：<a href="https://npmmirror.com/">npmmirror 中国镜像站</a></p>
<h4 id="electronjs-镜像和缓存"><a aria-hidden="true" tabindex="-1" href="#electronjs-镜像和缓存"><span class="icon icon-link"></span></a>electronjs 镜像和缓存</h4>
<pre class="wrap-text "><code class="language-ini code-highlight"><span class="code-line"><span class="token key attr-name">ELECTRON_MIRROR</span><span class="token punctuation">=</span><span class="token value attr-value">"<span class="token inner-value">https://npmmirror.com/mirrors/electron/</span>"</span>
</span><span class="code-line"><span class="token key attr-name">ELECTRON_BUILDER_BINARIES_MIRROR</span><span class="token punctuation">=</span><span class="token value attr-value">https://npmmirror.com/mirrors/electron-builder-binaries/</span>
</span><span class="code-line"><span class="token comment">; ELECTRON_CUSTOM_DIR="{{ version }}"</span>
</span></code></pre>
<!--rehype:className=wrap-text -->
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="身份验证相关配置"><a aria-hidden="true" tabindex="-1" href="#身份验证相关配置"><span class="icon icon-link"></span></a>身份验证相关配置</h3><div class="wrap-body">
<pre class="language-ini"><code class="language-ini code-highlight"><span class="code-line"><span class="token key attr-name">//registry.npmjs.org/:_authToken</span><span class="token punctuation">=</span><span class="token value attr-value">MYTOKEN</span>
</span><span class="code-line"><span class="token comment">; 将适用于 @myorg 和 @another</span>
</span><span class="code-line"><span class="token key attr-name">//somewhere.com/:_authToken</span><span class="token punctuation">=</span><span class="token value attr-value">MYTOKEN</span>
</span><span class="code-line"><span class="token comment">; 将适用于 @myorg</span>
</span><span class="code-line"><span class="token key attr-name">//somewhere.com/myorg/:_authToken</span><span class="token punctuation">=</span><span class="token value attr-value">MYTOKEN1</span>
</span><span class="code-line"><span class="token comment">; 将适用于 @another</span>
</span><span class="code-line"><span class="token key attr-name">//somewhere.com/another/:_authToken</span><span class="token punctuation">=</span><span class="token value attr-value">MYTOKEN2</span>
</span></code></pre>
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="纯-esm-包"><a aria-hidden="true" tabindex="-1" href="#纯-esm-包"><span class="icon icon-link"></span></a>纯 ESM 包</h2><div class="wrap-body">
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="commonjs-项目移动到-esm"><a aria-hidden="true" tabindex="-1" href="#commonjs-项目移动到-esm"><span class="icon icon-link"></span></a>CommonJS 项目移动到 ESM</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->
<ul>
<li>将 <code>"type": "module"</code> 添加到您的 <a href="./package.json.html">package.json</a></li>
<li>将 <a href="./package.json.html">package.json</a> 中的 <code>"main": "index.js"</code> 替换为 <code>"exports": "./index.js"</code>。</li>
<li>将 <a href="./package.json.html">package.json</a> 中的 <code>"engines"</code> 字段更新为 Node.js 14: <code>"node": ">=14.16"</code>。(不包括 <red><del>Node.js 12</del></red>，因为它不再受支持)</li>
<li>删除 <code>"use strict"</code>；来自所有 JavaScript 文件</li>
<li>将所有 <code>require()</code> / <code>module.export</code> 替换为 <code>import</code> / <code>export</code></li>
<li>仅使用完整的相对文件路径进行导入：<code>import x from '.';</code> → <code>import x from './index.js';</code></li>
<li>如果您有 <code>TypeScript</code> 类型定义（例如 <code>index.d.ts</code>），请将其更新为使用 ESM 导入/导出</li>
<li>可选但推荐使用 <code>node:</code> 导入<a href="https://nodejs.org/api/esm.html#esm_node_imports">协议</a></li>
</ul>
</div></div></div><div class="wrap h3body-not-exist col-span-3"><div class="wrap-header h3wrap"><h3 id="typescript-项目输出-esm"><a aria-hidden="true" tabindex="-1" href="#typescript-项目输出-esm"><span class="icon icon-link"></span></a>TypeScript 项目输出 ESM</h3><div class="wrap-body">
<!--rehype:wrap-class=col-span-3-->
<ul>
<li>确保您使用的是 TypeScript 4.7 或更高版本</li>
<li>将 <code>"type": "module"</code> 添加到您的 <a href="./package.json.html">package.json</a></li>
<li>将 <a href="./package.json.html">package.json</a> 中的 <code>"main": "index.js"</code> 替换为 <code>"exports": "./index.js"</code></li>
<li>将 <a href="./package.json.html">package.json</a> 中的 <code>"engines"</code> 字段更新为 Node.js 14: <code>"node": ">=14.16"</code>。 （不包括 <red><del>Node.js 12</del></red>，因为它不再受支持）</li>
<li>将 <code>"module": "node16"</code>, <code>"moduleResolution": "node16"</code> 添加到您的 <a href="./typescript.html">tsconfig.json</a> (<a href="https://github.com/sindresorhus/tsconfig/blob/main/tsconfig.json">列子</a>)</li>
<li>仅使用完整的相对文件路径进行导入：<code>import x from '.';</code> → <code>import x from './index.js';</code></li>
<li>删除 <code>namespace</code> 使用并改用 <code>export</code></li>
<li>可选但推荐使用 <code>node:</code> 导入<a href="https://nodejs.org/api/esm.html#esm_node_imports">协议</a></li>
<li><strong>即使您正在导入 <code>.ts</code> 文件，也必须在相对导入中使用 <code>.js</code> 扩展名</strong></li>
</ul>
<p>阅读<a href="https://www.typescriptlang.org/docs/handbook/esm-node.html">官方 ESM 指南</a></p>
</div></div></div></div></div><div class="wrap h2body-not-exist"><div class="wrap-header h2wrap"><h2 id="另见"><a aria-hidden="true" tabindex="-1" href="#另见"><span class="icon icon-link"></span></a>另见</h2><div class="wrap-body">
<ul>
<li><a href="https://docs.npmjs.com/">npm 仓库、网站和命令行界面的文档</a> <em>(npmjs.com)</em></li>
<li><a href="https://npmmirror.com/">npmmirror 中国镜像站</a> <em>(npmmirror.com)</em></li>
<li><a href="https://michaelcurrin.github.io/dev-cheatsheets/cheatsheets/package-managers/javascript/npm/commands/npx.html">Dev Cheatsheets npx</a> <em>(michaelcurrin.github.io)</em></li>
</ul>
</div></div><div class="h2wrap-body"></div></div></div><script src="https://giscus.app/client.js" data-repo="jaywcjlove/reference" data-repo-id="R_kgDOID2-Mw" data-category="Q&#x26;A" data-category-id="DIC_kwDOID2-M84CS5wo" data-mapping="pathname" data-strict="0" data-reactions-enabled="1" data-emit-metadata="0" data-input-position="bottom" data-theme="dark" data-lang="zh-CN" crossorigin="anonymous" async></script><div class="giscus"></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang.</footer></footer><script src="../data.js?v=1.5.2" defer></script><script src="../js/fuse.min.js?v=1.5.2" defer></script><script src="../js/main.js?v=1.5.2" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
  <path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
</html>
